package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonnelPalmprint;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoPersonnelInfoVo;
import com.ocom.payhandle.service.PersonnelInfoVoService;
import com.ocom.payhandle.vo.PersonnelInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class PersonnelInfoVoServiceImpl extends ServiceImpl<IDaoPersonnelInfoVo, PersonnelInfoVo> implements PersonnelInfoVoService {

    @Autowired
    private IDaoPersonnelInfoVo iDaoPersonnelInfoVo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<PersonnelInfoVo> selectCard(long companyId, List<Integer> courseIds) {
        List<PersonnelInfoVo> list = iDaoPersonnelInfoVo.selectCard(courseIds);
        if(list == null || list.size() == 0){
            return list;
        }
        for (int i =0;i<list.size();i++){
            PersonnelInfoVo personnelInfoVo= list.get(i);
            if(personnelInfoVo.getCardState()==null){
                personnelInfoVo.setCardState(5);
                list.set(i,personnelInfoVo);
                continue;
            }
            if(personnelInfoVo.getCardState()==0){
                personnelInfoVo.setCardState(1);
            }
            if(personnelInfoVo.getCardState()==1){
                personnelInfoVo.setCardState(0);
            }
            list.set(i,personnelInfoVo);
        }
        return list;
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<PersonnelInfoVo> selectPersonnelInfoCard(long companyId, List<Integer> courseIds) {
        return iDaoPersonnelInfoVo.selectPersonnelInfoCard(courseIds);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<PersonnelInfoVo> selectPersonnelInfoUrl(long companyId, List<Integer> courseIds, String version) {
        List<PersonnelInfoVo> personnelInfoVos = iDaoPersonnelInfoVo.selectPersonnelInfoUrl(companyId, courseIds, version);
        List<PersonnelInfoVo> personnelInfoVos1 = iDaoPersonnelInfoVo.selectPersonnelInfoUrl2(companyId, courseIds, version);

        Map<Integer, PersonnelInfoVo> mergedMap = Stream.concat(personnelInfoVos.stream(), personnelInfoVos1.stream())
                .collect(Collectors.toMap(
                        PersonnelInfoVo::getPId,
                        Function.identity(),
                        (existing, replacement) -> existing.mergeWith(replacement)
                ));

        // 明确转换为新的ArrayList，避免并发问题
        List<PersonnelInfoVo> mergedList = new ArrayList<>(mergedMap.values());
        return mergedList;
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityPersonnelPalmprint> getPalmByPIds(long companyId, List<Integer> pIds) {
        return iDaoPersonnelInfoVo.getPalmByPIds(companyId, pIds);
    }
}
